package org.example;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
//        writeFile();
        readFile();
    }

    /**
     * 文件写入操作
     */
    public static void writeFile() {
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter("/Users/cengyunke/Desktop/atm1/IOWork/src/main/java/org/example/File/poem.txt");
            fileWriter.write("窗前明月光，疑是地上霜。举头望明月，低头思故乡。");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void readFile() {
        String poem = "";
        FileReader fileReader = null;
        int index = 0;
        try {
            fileReader = new FileReader("/Users/cengyunke/Desktop/atm1/IOWork/src/main/java/org/example/File/poem.txt");
            fileReader.read();
            while ((index = fileReader.read()) != -1) {
                poem += (char) index;
            }
            System.out.println(poem);

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}